using System;
using System.Collections.Generic;
using System.Text;

using System.Data;
using System.Data.Common;
using Garlic.CC.Utility;
using Microsoft.Practices.EnterpriseLibrary.Data;
using Microsoft.Practices.EnterpriseLibrary.Data.Sql;

using Garlic.DM.DataModel;

namespace Garlic.DAL
{
    /// <summary>
    /// DataAccess
    /// </summary>
    public partial class CodeDetailDAL
    {
        #region Sql const
        public const string TC_BAS_CODEDETAIL_SELECT_BY_ID = " SELECT code_value,value_cdescription,value_edescription,code_name,create_user,create_time,modify_user,modify_time" +
                                            " FROM TC_BAS_CodeDetail" +
                                            " WHERE 1=1 " +
                                            " and  code_value =  :code_value" +
                                            " and  code_name =  :code_name";


        public const string TC_BAS_CODEDETAIL_SELECT = " SELECT " +
                                            "code_value," +
                                            "value_cdescription," +
                                            "value_edescription," +
                                            "code_name," +
                                            "create_user," +
                                            "create_time," +
                                            "modify_user," +
                                            "modify_time" +
                                            " FROM TC_BAS_CodeDetail {0}";



        public const string TC_BAS_CODEDETAIL_INSERT = " INSERT INTO TC_BAS_CodeDetail (" +
                                            "code_value," +
                                            "value_cdescription," +
                                            "value_edescription," +
                                            "code_name," +
                                            "create_user," +
                                            "create_time," +
                                            "modify_user," +
                                            "modify_time" +
                                        ") VALUES (" +
                                            ":code_value," +
                                            ":value_cdescription," +
                                            ":value_edescription," +
                                            ":code_name," +
                                            ":create_user," +
                                            ":create_time," +
                                            ":modify_user," +
                                            ":modify_time" +
                                        ")";


        public const string TC_BAS_CODEDETAIL_UPDATE = " UPDATE TC_BAS_CodeDetail SET " +
                                            " value_cdescription =  :value_cdescription, " +
                                            " value_edescription =  :value_edescription, " +
                                            " create_user =  :create_user, " +
                                            " create_time =  :create_time, " +
                                            " modify_user =  :modify_user, " +
                                            " modify_time =  :modify_time " +
                                        " WHERE 1=1 " +
                                            " and  code_value =  :code_value" +
                                            " and  code_name =  :code_name";



        public const string TC_BAS_CODEDETAIL_DELETE = " DELETE FROM TC_BAS_CodeDetail" +
                                        " WHERE 1=1" +
                                            " and  code_value =  :code_value" +
                                            " and  code_name =  :code_name";



        #endregion

        /// <summary>
        /// Get CodeDetailInfo by code_value
        /// </summary>
        /// <param name="code_value">CodeDetailInfo </param>
        /// <returns></returns>
        public static CodeDetailInfo GetInfo(string code_value, string code_name)
        {

            Database db = DatabaseFactory.CreateDatabase();
            DbCommand dbCommand = db.GetSqlStringCommand(TC_BAS_CODEDETAIL_SELECT_BY_ID);
            db.AddInParameter(dbCommand, "code_value", DbType.AnsiString, code_value);
            db.AddInParameter(dbCommand, "code_name", DbType.AnsiString, code_name);


            using (IDataReader dr = db.ExecuteReader(dbCommand))
            {
                if (dr.Read())
                {
                    return CreateCodeDetailInfo(dr);
                }
                else
                    return null;
            }
        }


        /// <summary>
        /// Get data list
        /// </summary>
        /// <param name="textWhere">conditon</param>
        /// <param name="orderText">sort</param>
        /// <returns>CodeDetailInfo list </returns>
        public static List<CodeDetailInfo> GetList(string textWhere, string orderText)
        {
            string query = string.Empty;
            if (!string.IsNullOrEmpty(textWhere))
            {
                query += " where " + textWhere;
            }

            if (!string.IsNullOrEmpty(orderText))
            {
                query += " order by " + orderText;
            }

            Database db = DatabaseFactory.CreateDatabase();
            DbCommand dbCommand = db.GetSqlStringCommand(string.Format(TC_BAS_CODEDETAIL_SELECT, query));

            return GetList(string.Format(TC_BAS_CODEDETAIL_SELECT, query));

        }

        /// <summary>
        /// Get data list
        /// </summary>
        /// <param name="sql">SQL statement</param>
        /// <returns>CodeDetailInfo list </returns>
        public static List<CodeDetailInfo> GetList(string sql)
        {
            Database db = DatabaseFactory.CreateDatabase();
            DbCommand dbCommand = db.GetSqlStringCommand(sql);
            List<CodeDetailInfo> list = new List<CodeDetailInfo>();
            using (IDataReader dr = db.ExecuteReader(dbCommand))
            {
                while (dr.Read())
                {
                    list.Add(CreateCodeDetailInfo(dr));
                }
            }

            return list;
        }


        /// <summary>
        /// Add
        /// </summary>
        /// <param name="codeDetailInfo"> entity</param>
        public static int Add(CodeDetailInfo codeDetailInfo)
        {
            Database db = DatabaseFactory.CreateDatabase();

            DbCommand dbCommand = db.GetSqlStringCommand(TC_BAS_CODEDETAIL_INSERT);

            db.AddInParameter(dbCommand, "code_value", DbType.AnsiString, codeDetailInfo.Code_value);
            db.AddInParameter(dbCommand, "value_cdescription", DbType.AnsiString, DBConvert.ToDBValue(codeDetailInfo.Value_cdescription));
            db.AddInParameter(dbCommand, "value_edescription", DbType.AnsiString, DBConvert.ToDBValue(codeDetailInfo.Value_edescription));
            db.AddInParameter(dbCommand, "code_name", DbType.AnsiString, codeDetailInfo.Code_name);
            db.AddInParameter(dbCommand, "create_user", DbType.Int32, DBConvert.ToDBValue(codeDetailInfo.Create_user));
            db.AddInParameter(dbCommand, "create_time", DbType.DateTime, DBConvert.ToDBValue(codeDetailInfo.Create_time));
            db.AddInParameter(dbCommand, "modify_user", DbType.Int32, DBConvert.ToDBValue(codeDetailInfo.Modify_user));
            db.AddInParameter(dbCommand, "modify_time", DbType.DateTime, DBConvert.ToDBValue(codeDetailInfo.Modify_time));

            return int.Parse("0" + db.ExecuteScalar(dbCommand));
        }

        /// <summary>
        /// Update
        /// </summary>
        /// <param name="codeDetailInfo"> utility</param>
        public static void Update(CodeDetailInfo codeDetailInfo)
        {
            Database db = DatabaseFactory.CreateDatabase();

            DbCommand dbCommand = db.GetSqlStringCommand(TC_BAS_CODEDETAIL_UPDATE);

            db.AddInParameter(dbCommand, "code_value", DbType.AnsiString, codeDetailInfo.Code_value);
            db.AddInParameter(dbCommand, "value_cdescription", DbType.AnsiString, DBConvert.ToDBValue(codeDetailInfo.Value_cdescription));
            db.AddInParameter(dbCommand, "value_edescription", DbType.AnsiString, DBConvert.ToDBValue(codeDetailInfo.Value_edescription));
            db.AddInParameter(dbCommand, "code_name", DbType.AnsiString, codeDetailInfo.Code_name);
            db.AddInParameter(dbCommand, "create_user", DbType.Int32, DBConvert.ToDBValue(codeDetailInfo.Create_user));
            db.AddInParameter(dbCommand, "create_time", DbType.DateTime, DBConvert.ToDBValue(codeDetailInfo.Create_time));
            db.AddInParameter(dbCommand, "modify_user", DbType.Int32, DBConvert.ToDBValue(codeDetailInfo.Modify_user));
            db.AddInParameter(dbCommand, "modify_time", DbType.DateTime, DBConvert.ToDBValue(codeDetailInfo.Modify_time));

            db.ExecuteNonQuery(dbCommand);
        }

        /// <summary>
        /// Delete
        /// </summary>
        /// <param name="code_value,code_name">TC_BAS_CodeDetail Number</param>
        /// <returns></returns>
        public static void Delete(string code_value, string code_name)
        {
            Database db = DatabaseFactory.CreateDatabase();
            DbCommand dbCommand = db.GetSqlStringCommand(TC_BAS_CODEDETAIL_DELETE);
            db.AddInParameter(dbCommand, "code_value", DbType.AnsiString, code_value);
            db.AddInParameter(dbCommand, "code_name", DbType.AnsiString, code_name);
            db.ExecuteNonQuery(dbCommand);
        }




        private static CodeDetailInfo CreateCodeDetailInfo(IDataReader rdr)
        {
            CodeDetailInfo mycodeDetailInfo = new CodeDetailInfo();
            mycodeDetailInfo.Code_value = rdr.GetString(0);
            mycodeDetailInfo.Code_name = rdr.GetString(3);
            mycodeDetailInfo.Value_cdescription = DBConvert.GetString(rdr, 1);
            mycodeDetailInfo.Value_edescription = DBConvert.GetString(rdr, 2);
            
            mycodeDetailInfo.Create_user = DBConvert.GetInt32(rdr, 4);
            mycodeDetailInfo.Create_time = DBConvert.GetDateTime(rdr, 5);
            mycodeDetailInfo.Modify_user = DBConvert.GetInt32(rdr, 6);
            mycodeDetailInfo.Modify_time = DBConvert.GetDateTime(rdr, 7);

            return mycodeDetailInfo;
        }
    }
}
